package com.syc.springsecuritydemo1.config;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义登录成功逻辑
 * @author s
 */
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    private final String url;

    public MyAuthenticationSuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //可以在此处返回json，token
        response.sendRedirect(url);
        //权限
        System.out.println(authentication.getAuthorities());
        //凭证 基于安全考虑，凭证不会显示，会显示null
        System.out.println(authentication.getCredentials());
        //详情
        System.out.println(authentication.getDetails());
        //user对象
        System.out.println(authentication.getPrincipal());
    }
}
